
import plugin from '../../../../lib/plugins/plugin.js';
import PlayerManager from '../model/player/player_manager.js';
import Fight from '../service/fight.js';
import Signin from '../service/signin.js';


export const pm = PlayerManager.getInstance();


export function getPlayerByE(e: { user_id: number; }) {
    return pm.getPlayerByQQNum(e.user_id);
}



export class LNERobot extends plugin {
    playerManager: PlayerManager;
    constructor() {
        super({
            name: '机器人',
            dsc: '机器人',
            event: 'message',
            priority: 5,
            rule: [
                {
                    /** 命令正则匹配 */
                    reg: '^签到$',
                    /** 执行方法 */
                    fnc: 'signin'
                },
                {
                    /** 命令正则匹配 */
                    reg: '^保存$',
                    /** 执行方法 */
                    fnc: 'save'
                },
                {
                    /** 命令正则匹配 */
                    reg: '^攻击.*$',
                    /** 执行方法 */
                    fnc: 'hit'
                },
                {
                    /** 命令正则匹配 */
                    reg: '^改名 .*$',
                    /** 执行方法 */
                    fnc: 'rename'
                },
                {
                    /** 命令正则匹配 */
                    reg: '^菜单$',
                    /** 执行方法 */
                    fnc: 'menu'
                },
                {
                    /** 命令正则匹配 */
                    reg: '^决斗.*$',
                    /** 执行方法 */
                    fnc: 'duel'
                },
            ]
        });

        this.playerManager = pm;
    }

    async menu() {
        await super.reply(
            `菜单
- 改名 [名字]
- 签到
- 猜拳
- 打工
- 决斗
- 商店
- 信息
- 我的物品`
        );
    }

    async duel(e) {
        if (isNaN(e.at)) {
            super.reply("请长按头像艾特，不能复制！");
            return;
        }
        let p1 = await getPlayerByE(e);
        let p2 = await this.playerManager.getPlayerByQQNum(e.at);
        let fight = new Fight(p1, p2);


        try {
            let result = fight.duel();
            await this.playerManager.savePlayer(p1);
            await this.playerManager.savePlayer(p2);

            let rpl = "";
            let flag = false;
            for (let i in result.records) {
                let count = Number.parseInt(i);
                if (count < 3 || count >= result.records.length - 3) rpl += `第${count + 1}回合\n${result.records[i].msg}\n\n`;
                else if (!flag) {
                    flag = true;
                    rpl += `...\n`;
                }
            }
            rpl += "最终的获胜者是\n【" + result.winner.name + "】！";
            await super.reply(rpl);
        } catch (err) {
            super.reply(err.message);
        }
    }

    async rename(e) {
        let p = await getPlayerByE(e);
        p.nick = e.msg.split(" ")[1];
        await this.playerManager.savePlayer(p);
        super.reply(`成功改名为${p.nick}`);
    }

    async hit(e) {
        if (isNaN(e.at)) {
            super.reply("请长按头像艾特，不能复制！");
            return;
        }
        let player = await getPlayerByE(e);
        let p = await this.playerManager.getPlayerByQQNum(e.at);
        await super.reply(new Fight(player, p).fight(true));

        await this.playerManager.savePlayer(player);
        await this.playerManager.savePlayer(p);
    }


    async save() {
        // this.playerManager.playersMap.forEach((player, qqNum) =>
        //     this.playerManager.savePlayer(player));
    }

    async signin(e) {
        let player = await getPlayerByE(e);
        try {
            await super.reply(await new Signin().signin(player) + '\n' +
                `你现在共有${player.coin}个硬币`);
            await this.playerManager.savePlayer(player);
        } catch (err) {
            await super.reply(err.message);
        }
    }


    /** 复读 */
    async repeat() {
        /** 设置上下文，后续接收到内容会执行doRep方法 */
        super.setContext('doRep');
        /** 回复 */
        await super.reply('请发送要复读的内容', false, { at: true });
    }

    /** 接受内容 */
    doRep() {
        /** 复读内容 */
        super.reply(super.e.message, false, { recallMsg: 5 });
        /** 结束上下文 */
        super.finish('doRep');
    }
}
